/*
 *  Copyright (C) 2010 Fabien Renaud
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.boc.botv.dao.impl;

import com.boc.botv.common.Database;
import com.boc.botv.dao.UserDao;
import com.boc.botv.model.User;
import java.util.List;
import org.springframework.stereotype.Repository;

/**
 *
 * @author Fabien Renaud
 */
@Repository("userDao")
public class UserDaoImpl implements UserDao {

  public List<User> getUsers() {
    return (List<User>) Database.getSession().createQuery("from User").list();
  }

  public boolean loginExists(String login) {
    return !Database.getSession().createQuery("from User as user where user.login = '" + login + "'").list().isEmpty();
  }

  public void updateUser(User user) {
    Database.getSession().update(user);
  }

  public User getUser(String login) {
    List<User> lu = (List<User>) Database.getSession().createQuery("from User as user where user.login = '" + login + "'").list();
    if (lu.size() != 1) {
      return null;
    }
    User u = lu.get(0);
    return u;
  }

  public User getUser(int userId) {
    return (User) Database.getSession().load(User.class, userId);
  }

  public void saveUser(User user) {
    Database.getSession().save(user);
  }

  public boolean mailExists(String mail) {
    return !Database.getSession().createQuery("from User as user where user.email = '" + mail + "'").list().isEmpty();
  }

  public void deleteUser(User user) {
    Database.getSession().delete(user);
  }
}
